<?php
require_once ("db/AbstractDb.php");
require_once ("model/Product.php");

class ProductDAO extends AbstractDb {

	private function createProductInternal($pole) {

		$result = new Product();

		$result->setProductId($pole["idp"]);
		$result->setProductName($pole["jmeno"]);
		$result->setProductShortDesc($pole["kpopisek"]);
		$result->setProductLongDesc($pole["dpopisek"]);
		$result->setProductPrice($pole["cena"]);
		$result->setProductCategory($pole["cat_id"]);

		return $result;
	}

	public function loadProduct($id) {

		$result = $this->query("SELECT * FROM products WHERE idp = " . $id);

		$product = new Product();

		//zpracovani resultu a predelani do objektu..
		while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {

			$product = $this->createProductInternal($row);
		}

		return $product;
	}

	public function loadProductListByIdCategory($idCat) {

		$queryString = "SELECT * FROM products WHERE cat_id =" . $idCat;

		$result = $this->query($queryString);

		$return = array ();

		while ($row = mysql_fetch_array($result)) {
			$return[] = $this->createProductInternal($row);
		}

		return $return;

	}

	public function loadProductListByIdCategoryLimit($idCat, $od, $do) {

		$queryString = "SELECT * FROM products WHERE cat_id =" . $idCat . " LIMIT " . $od . "," . $do;

		$result = $this->query($queryString);

		$return = array ();

		if ($this->getAffectedRows() > 0) {

			while ($row = mysql_fetch_array($result)) {
				$return[] = $this->createProductInternal($row);
			}

		} else {
			return null;
		}

		return $return;

	}
	
	public function getCountOfProducts($id){
		
		$queryString = "SELECT * FROM products WHERE cat_id =" . $id;
		$result = $this->query($queryString);
		
		return $this->getAffectedRows();
		
	}

}
?>
